const ShoppingCart = require("../data/ShoppingCart");
const { Op } = require("sequelize");
module.exports = {
  // 获取购物车信息
  async GetShoppingCart(user_id) {
    const ins = await ShoppingCart.findAll({
      where: {
        user_id,
      },
    });
    return JSON.parse(JSON.stringify(ins));
  },
  // 查询用户的购物车的某个商品
  async FindShoppingCart(user_id, product_id) {
    const ins = await ShoppingCart.findAll({
      where: {
        user_id,
        product_id,
      },
    });
    return JSON.parse(JSON.stringify(ins));
  },
  // 新插入购物车信息
  async AddShoppingCart(user_id, product_id) {
    const ins = await ShoppingCart.create({
      user_id,
      product_id,
      num:1
    });
    return ins?ins.toJSON():null;
  },
  // 更新购物车商品数量
  async UpdateShoppingCart(NewNum, user_id, product_id) {
    const ins = await ShoppingCart.update(
      { num: NewNum },
      {
        where: {
          user_id,
          product_id,
        },
      }
    );
    return ins;
  },
  // 删除购物车信息
  async DeleteShoppingCart(user_id, product_id) {
    const ins = await ShoppingCart.destroy({
      //通过模型实例（类似于数据表）删除。
      where: {
        user_id,
        product_id,
      },
    });
    return ins;
  },
};
